package com.yunqing.restkeeper.enterprise.web.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.yunqing.restkeeper.common.utils.Result;
import com.yunqing.restkeeper.common.utils.ResultCode;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName FileUploadController
 * @Description TODO
 * @author: puyunqing
 * @Date: 2025/7/3
 */
@Tag(name = "图片上传通用接口")
@RestController
@RefreshScope
public class FileUploadController {

    @Resource
    private OSS ossClient;

    @Value("${bucketName}")
    private String bucketName;

    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endpoint;


    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile multipartFile) {

        Result result = new Result();

        //设置图片上传的文件名称
        String fileName = System.currentTimeMillis() + "_" + multipartFile.getOriginalFilename();

        try {
            ossClient.putObject(bucketName, fileName, multipartFile.getInputStream());

            //拼接文件路径进行返回
            String logoPath = "https://" + bucketName + "." + endpoint + "/" + fileName;

            result.setData(logoPath);
            result.setStatus(ResultCode.success);
            result.setDesc("上传成功");
            return result;

        } catch (IOException e) {
            e.printStackTrace();
            result.setStatus(ResultCode.error);
            result.setDesc("上传失败");
            return result;
        }
    }

    @PostMapping("/imageUploadResize")
    @Parameter(schema = @Schema(type = "file"), name = "fileName", description = "上传文件", required = true)
    public Result imageUploadResize(@RequestParam("fileName") MultipartFile multipartFile) {

        Result result = new Result();

        String fileName = System.currentTimeMillis() + "_" + multipartFile.getOriginalFilename();

        //图片上传
        try {
            ossClient.putObject(bucketName, fileName, multipartFile.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(ResultCode.error);
            result.setDesc("图片上传失败");
            return result;
        }

        String imagePath = "https://" + bucketName + "." + endpoint + "/" + fileName + "?x-oss-process=image/resize,m_fill,h_100,w_200";

        result.setStatus(ResultCode.success);
        result.setDesc("图片上传成功");
        result.setData(imagePath);

        return result;
    }
}
